<h1><%= @info.app.name %> API Documentation</h1>

<%= content_tag :p, @info.app.title %>
<%= content_tag :p, @info.app.description %>
<p>
This document describes the <%= @info.app.name %> API interface. It is designed for developers
who wants to interoperate with <%= @info.app.name %> in their own application.
</p>

<div class="toc">
  <h2 class="title">Table of content</h2>

  <% @info.toc.keys.sort.each do |controller_name| %>
    <%= content_tag :h3, controller_name.gsub(%r{/}, ' ').titleize %>
    <table>
      <% @info.toc[controller_name].keys.sort.each do |action_name| %>
        <% route = @info.toc[controller_name][action_name] %>
        <tr>
          <%= content_tag :td do %>
            <%= link_to route.spec, doc_path(anchor: route.anchor) %>
          <% end %>
          <%= content_tag :td, route.schema.title, width: "60%" %>
        </tr>
      <% end %>
    </table>
  <% end %>
</div>

<hr>

<div class="route">
  <%
  @info.toc.keys.sort.each do |controller_name|
    @info.toc[controller_name].keys.sort.each do |action_name|

      route = @info.toc[controller_name][action_name]
      action_schema = route.schema
  %>
      <h2 class="title">
        <%= content_tag :a, route.spec, id: route.anchor %>
      </h2>

      <%= content_tag :p, action_schema.title %>
      <%= content_tag :p, action_schema.description %>

      <%
      if action_schema.request
        request_schema = action_schema.request
      %>
        <h3>Request</h3>
        <% unless request_schema.headers.documented_properties.empty? %>
          <h4>Headers</h4>

          <table>
          <% request_schema.headers.each do |name, schema| %>
             <tr>
               <td width="30%">
                <% render "infos", schema: schema, name: name %>
               </td>
             </tr>
          <% end %>
          </table>

          <%= toggler "Show headers schema" do %>
            <%= highlight_json_schema request_schema.headers.to_h %>
          <% end %>
        <% end %>

        <%
        [ :path, :query, :body ].each do |name|
          params_schema = request_schema.send("#{name}_parameters")
           unless params_schema.documented_properties.empty? %>
             <h4><%= name.capitalize %> parameters</h4>

             <table>
             <% flat_each(params_schema) do |path, schema| %>
                <% next unless schema.documented? %>
                <tr>
                  <td width="30%">
                    <% render "infos", schema: schema, name: name %>
                  </td>
                </tr>
             <% end %>
             </table>

             <%= toggler "Show #{name} parameters schema" do %>
               <%= highlight_json_schema params_schema.to_h %>
             <% end %>
           <% end %>
        <% end %>
      <% end %>

      <% unless action_schema.responses.empty? %>
        <% action_schema.responses.each do |status, response_schema| %>
          <h3>Response <%= response_schema.http_status %> [ <%= response_schema.status %> ]</h3>
          <%= content_tag :p, response_schema.title %>
          <%= content_tag :p, response_schema.description %>
          <% unless response_schema.headers.documented_properties.empty? %>
            <h4>Headers</h4>
            <%= highlight_json_schema response_schema.headers.to_h %>
          <% end %>
          <% if response_schema.body %>
            <h4>Body</h4>
            <%= highlight_json_schema response_schema.body.to_h %>
          <% end %>
        <% end %>
      <% end %>

    <% end %>
  <% end %>
</div>
